FlatKitOutline.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using UnityEngine;
  2. using UnityEngine.Rendering.Universal;
  3. #if UNITY_2022_3_OR_NEWER
  4. using ExternPropertyAttributes;
  5. namespace FlatKit {
  6. public class FlatKitOutline : ScriptableRendererFeature {
  7. [Tooltip("To create new settings use 'Create > FlatKit > Outline Settings'.")]
  8. [Expandable]
  9. public OutlineSettings settings;
  10. private Material _effectMaterial;
  11. private DustyroomRenderPass _fullScreenPass;
  12. private bool _requiresColor;
  13. private bool _injectedBeforeTransparents;
  14. private ScriptableRenderPassInput _requirements = ScriptableRenderPassInput.Color;
  15. private const string ShaderName = "Hidden/FlatKit/OutlineWrap";
  16. private static int edgeColor => Shader.PropertyToID("_EdgeColor");
  17. private static int thickness => Shader.PropertyToID("_Thickness");
  18. private static int depthThresholdMin => Shader.PropertyToID("_DepthThresholdMin");
  19. private static int depthThresholdMax => Shader.PropertyToID("_DepthThresholdMax");
  20. private static int normalThresholdMin => Shader.PropertyToID("_NormalThresholdMin");
  21. private static int normalThresholdMax => Shader.PropertyToID("_NormalThresholdMax");
  22. private static int colorThresholdMin => Shader.PropertyToID("_ColorThresholdMin");
  23. private static int colorThresholdMax => Shader.PropertyToID("_ColorThresholdMax");
  24. private static int fadeRangeStart => Shader.PropertyToID("_FadeRangeStart");
  25. private static int fadeRangeEnd => Shader.PropertyToID("_FadeRangeEnd");
  26. public override void Create() {
  27. // Settings.
  28. {
  29. if (settings == null) return;
  30. settings.onSettingsChanged = null;
  31. settings.onReset = null;
  32. settings.onSettingsChanged += SetMaterialProperties;
  33. settings.onReset += SetMaterialProperties;
  34. }
  35. // Material.
  36. {
  37. #if UNITY_EDITOR
  38. settings.effectMaterial = SubAssetMaterial.GetOrCreate(settings, ShaderName);
  39. if (settings.effectMaterial == null) return;
  40. #endif
  41. _effectMaterial = settings.effectMaterial;
  42. SetMaterialProperties();
  43. }
  44. {
  45. _fullScreenPass = new DustyroomRenderPass {
  46. renderPassEvent = settings.renderEvent,
  47. };
  48. _requirements = ScriptableRenderPassInput.Color; // Needed for the full-screen blit.
  49. if (settings.useDepth) _requirements |= ScriptableRenderPassInput.Depth;
  50. if (settings.useNormals) _requirements |= ScriptableRenderPassInput.Normal;
  51. if (settings.fadeWithDistance) _requirements |= ScriptableRenderPassInput.Depth;
  52. ScriptableRenderPassInput modifiedRequirements = _requirements;
  53. _requiresColor = (_requirements & ScriptableRenderPassInput.Color) != 0;
  54. _injectedBeforeTransparents = settings.renderEvent <= RenderPassEvent.BeforeRenderingTransparents;
  55. if (_requiresColor && !_injectedBeforeTransparents) {
  56. modifiedRequirements ^= ScriptableRenderPassInput.Color;
  57. }
  58. _fullScreenPass.ConfigureInput(modifiedRequirements);
  59. }
  60. }
  61. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
  62. if (settings == null || !settings.applyInSceneView && renderingData.cameraData.isSceneViewCamera) return;
  63. if (renderingData.cameraData.isPreviewCamera) return;
  64. if (_effectMaterial == null) return;
  65. _fullScreenPass.Setup(_effectMaterial, _requiresColor, _injectedBeforeTransparents, "Flat Kit Outline",
  66. renderingData);
  67. renderer.EnqueuePass(_fullScreenPass);
  68. }
  69. protected override void Dispose(bool disposing) {
  70. _fullScreenPass?.Dispose();
  71. }
  72. private void SetMaterialProperties() {
  73. if (_effectMaterial == null) return;
  74. const string depthKeyword = "OUTLINE_USE_DEPTH";
  75. RendererFeatureUtils.SetKeyword(_effectMaterial, depthKeyword, settings.useDepth);
  76. const string normalsKeyword = "OUTLINE_USE_NORMALS";
  77. RendererFeatureUtils.SetKeyword(_effectMaterial, normalsKeyword, settings.useNormals);
  78. const string colorKeyword = "OUTLINE_USE_COLOR";
  79. RendererFeatureUtils.SetKeyword(_effectMaterial, colorKeyword, settings.useColor);
  80. const string outlineOnlyKeyword = "OUTLINE_ONLY";
  81. RendererFeatureUtils.SetKeyword(_effectMaterial, outlineOnlyKeyword, settings.outlineOnly);
  82. const string resolutionInvariantKeyword = "RESOLUTION_INVARIANT_THICKNESS";
  83. RendererFeatureUtils.SetKeyword(_effectMaterial, resolutionInvariantKeyword, settings.resolutionInvariant);
  84. const string fadeWithDistanceKeyword = "OUTLINE_FADE_OUT";
  85. RendererFeatureUtils.SetKeyword(_effectMaterial, fadeWithDistanceKeyword, settings.fadeWithDistance);
  86. _effectMaterial.SetColor(edgeColor, settings.edgeColor);
  87. _effectMaterial.SetFloat(thickness, settings.thickness);
  88. _effectMaterial.SetFloat(depthThresholdMin, settings.minDepthThreshold);
  89. _effectMaterial.SetFloat(depthThresholdMax, settings.maxDepthThreshold);
  90. _effectMaterial.SetFloat(normalThresholdMin, settings.minNormalsThreshold);
  91. _effectMaterial.SetFloat(normalThresholdMax, settings.maxNormalsThreshold);
  92. _effectMaterial.SetFloat(colorThresholdMin, settings.minColorThreshold);
  93. _effectMaterial.SetFloat(colorThresholdMax, settings.maxColorThreshold);
  94. _effectMaterial.SetFloat(fadeRangeStart, settings.fadeRangeStart);
  95. _effectMaterial.SetFloat(fadeRangeEnd, settings.fadeRangeEnd);
  96. }
  97. }
  98. }
  99. #else
  100. namespace FlatKit {
  101. public class FlatKitOutline : ScriptableRendererFeature {
  102. [Tooltip("To create new settings use 'Create > FlatKit > Outline Settings'.")]
  103. public OutlineSettings settings;
  104. [SerializeField, HideInInspector]
  105. private Material _effectMaterial;
  106. private BlitTexturePass _blitTexturePass;
  107. private static readonly string OutlineShaderName = "Hidden/FlatKit/OutlineFilter";
  108. private static readonly int EdgeColor = Shader.PropertyToID("_EdgeColor");
  109. private static readonly int Thickness = Shader.PropertyToID("_Thickness");
  110. private static readonly int DepthThresholdMin = Shader.PropertyToID("_DepthThresholdMin");
  111. private static readonly int DepthThresholdMax = Shader.PropertyToID("_DepthThresholdMax");
  112. private static readonly int NormalThresholdMin = Shader.PropertyToID("_NormalThresholdMin");
  113. private static readonly int NormalThresholdMax = Shader.PropertyToID("_NormalThresholdMax");
  114. private static readonly int ColorThresholdMin = Shader.PropertyToID("_ColorThresholdMin");
  115. private static readonly int ColorThresholdMax = Shader.PropertyToID("_ColorThresholdMax");
  116. public override void Create() {
  117. #if UNITY_EDITOR
  118. if (_effectMaterial == null) {
  119. SubAssetMaterial.AlwaysInclude(BlitTexturePass.CopyEffectShaderName);
  120. SubAssetMaterial.AlwaysInclude(OutlineShaderName);
  121. }
  122. #endif
  123. if (settings == null) {
  124. return;
  125. }
  126. if (!CreateMaterials()) {
  127. return;
  128. }
  129. SetMaterialProperties();
  130. _blitTexturePass ??=
  131. new BlitTexturePass(_effectMaterial, settings.useDepth, settings.useNormals, useColor: true);
  132. }
  133. protected override void Dispose(bool disposing) {
  134. _blitTexturePass.Dispose();
  135. }
  136. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
  137. #if UNITY_EDITOR
  138. if (renderingData.cameraData.isPreviewCamera) return;
  139. if (!settings.applyInSceneView && renderingData.cameraData.cameraType == CameraType.SceneView) return;
  140. #endif
  141. SetMaterialProperties();
  142. _blitTexturePass.Setup(renderingData);
  143. _blitTexturePass.renderPassEvent = settings.renderEvent;
  144. renderer.EnqueuePass(_blitTexturePass);
  145. }
  146. private bool CreateMaterials() {
  147. if (_effectMaterial == null) {
  148. var effectShader = Shader.Find(OutlineShaderName);
  149. var blitShader = Shader.Find(BlitTexturePass.CopyEffectShaderName);
  150. if (effectShader == null || blitShader == null) return false;
  151. _effectMaterial = UnityEngine.Rendering.CoreUtils.CreateEngineMaterial(effectShader);
  152. }
  153. return _effectMaterial != null;
  154. }
  155. private void SetMaterialProperties() {
  156. if (_effectMaterial == null) {
  157. return;
  158. }
  159. const string depthKeyword = "OUTLINE_USE_DEPTH";
  160. if (settings.useDepth) {
  161. _effectMaterial.EnableKeyword(depthKeyword);
  162. } else {
  163. _effectMaterial.DisableKeyword(depthKeyword);
  164. }
  165. const string normalsKeyword = "OUTLINE_USE_NORMALS";
  166. if (settings.useNormals) {
  167. _effectMaterial.EnableKeyword(normalsKeyword);
  168. } else {
  169. _effectMaterial.DisableKeyword(normalsKeyword);
  170. }
  171. const string colorKeyword = "OUTLINE_USE_COLOR";
  172. if (settings.useColor) {
  173. _effectMaterial.EnableKeyword(colorKeyword);
  174. } else {
  175. _effectMaterial.DisableKeyword(colorKeyword);
  176. }
  177. const string outlineOnlyKeyword = "OUTLINE_ONLY";
  178. if (settings.outlineOnly) {
  179. _effectMaterial.EnableKeyword(outlineOnlyKeyword);
  180. } else {
  181. _effectMaterial.DisableKeyword(outlineOnlyKeyword);
  182. }
  183. const string resolutionInvariantKeyword = "RESOLUTION_INVARIANT_THICKNESS";
  184. if (settings.resolutionInvariant) {
  185. _effectMaterial.EnableKeyword(resolutionInvariantKeyword);
  186. } else {
  187. _effectMaterial.DisableKeyword(resolutionInvariantKeyword);
  188. }
  189. _effectMaterial.SetColor(EdgeColor, settings.edgeColor);
  190. _effectMaterial.SetFloat(Thickness, settings.thickness);
  191. _effectMaterial.SetFloat(DepthThresholdMin, settings.minDepthThreshold);
  192. _effectMaterial.SetFloat(DepthThresholdMax, settings.maxDepthThreshold);
  193. _effectMaterial.SetFloat(NormalThresholdMin, settings.minNormalsThreshold);
  194. _effectMaterial.SetFloat(NormalThresholdMax, settings.maxNormalsThreshold);
  195. _effectMaterial.SetFloat(ColorThresholdMin, settings.minColorThreshold);
  196. _effectMaterial.SetFloat(ColorThresholdMax, settings.maxColorThreshold);
  197. }
  198. }
  199. }
  200. #endif